import QR from '/../../../utils/wxqrcode.js';
var ddapi = require('/../../../utils/md5.js');
const app = getApp()

Page({
  data: {
    score: 0,
    areacount: 0,
    ordercount: 0,
    level: '普通会员',
    nickname: '未知',
    avatar: '',
    qrcode: '',
    text: 'http://weixin.qq.com/r/JD-_5qPEov7dreeF92o2',
    display: '',
    wx_balance: 0,
  },
  onShow: function() {
  
    var that = this
    var user_phone = wx.getStorageSync('userPhone');
    if (user_phone) {
      console.log('12');
      that.loadInfo()
    } else {
      console.log('32');
      var url = '/pages/login/login';
      wx.navigateTo({
        url: url
      })
    }
    that.getlod();
  },

  myWallet:function(){
    wx.navigateTo({
      url: "/pages/my_wallet/index"
    });
  },
  relogin: function () {
    wx.navigateTo({
     url: "/pages/authorize/index"
    })
    // this.onLoad()
  },
  recharge: function () {
    wx.navigateTo({
      url: "/pages/my_wallet/index"
    })
  },
  withdraw: function () {
    wx.showModal({
      title: "提示",
      content: "提现功能已关闭",
      showCancel: false
    })
  },
  order: function() {
    wx.navigateTo({
      url: "/pages/ucenter/order_new_list/index"
    })
  },
  area: function () {
    wx.navigateTo({
      url: "/pages/ucenter/myarea/myarea"
    })
  },
  showview: function () {
    this.setData({
      display: "block"
    })
  },
  hideview: function () {
    this.setData({
      display: "none"
    })
  },
  getQRCodeSize: function () {
    var size = 0; try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 278; 
      var width = res.windowWidth / scale;
      size = width;

    } catch (e) {

    }
    return size;
  },
  createQRCode: function (text, size) {
    let that = this
    let _img = QR.createQrCodeImg(text, {
      size: parseInt(size)
    })
    
    that.setData({
      'qrcode': _img
    })
  },
  loadInfo: function() {
    var that = this
    var service = 'User.GetInfo';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.GetInfo',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        var code = res.data.data.code;
        if (code == 0) {
          var info = res.data.data.info[0];
          that.setData({
            score: info.user_balance,
            areacount: info.user_area_count,
            ordercount: info.user_order_count,
            level: info.user_level,
            nickname: info.nickname,
            avatar: info.user_avatar,
            wx_balance: info.wx_balance,
          })
        } 
      }
    })
    console.log(that.data)
  },

  getlod:function(){
    var that = this;
    var service = 'User.CheckLand';
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.CheckLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.user_phone = '';
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        }else{
          app.globalData.user_phone = '';
          wx.clearStorage();
          wx.navigateTo({
            url: "/pages/login/login"
          })
        }
      }
    })
  },
  
  Cancellation: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否注销当前账号',
      success: function (res) {
        if (res.confirm) {
          var service = 'User.CheckLand';
          var open_id = wx.getStorageSync('openid');
          var user_id = wx.getStorageSync('user_id');
          var sign = ddapi.md5(open_id + service + user_id);
          wx.request({
            url: app.globalData.apiUrl + '?service=User.CheckLand',
            header: {
              'Content-Type': 'json'
            },
            data: {
              sign: sign,
              user_id: user_id,
              open_id: open_id,
            },
            success: (res) => {
              var result = res.data.data;
              if (result.code == 0) {
                if (result.info == 1) {
                  that.editLand();
                } else {
                  app.globalData.user_phone = '';
                  wx.clearStorage();
                  wx.navigateTo({
                    url: "/pages/login/login"
                  })
                }
              } else {
                wx.showToast({
                  title: '注销失败',
                  icon: 'success',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },

  editLand: function () {
    var service = 'User.EditLand';
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var pid = 0;
    var sign = ddapi.md5(open_id + pid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.EditLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        pid: pid,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.user_phone = '';
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        } else {
          wx.showToast({
            title: '变更失败',
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  }
})